<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import navArrow from 'assets/images/right-icon.png';
    import defaultImg from 'assets/images/default-img.png';
    import api from 'api/api';
    import modal from 'utils/modal';
    import navigator from 'utils/navigator';
    import route from 'constants/route';
    import cartService from 'services/cartService';

    export default {
        data () {
            return {
                pageLoaded: false,
                defaultImg: defaultImg,
                navArrow: navArrow,

                /*页面参数*/
                shopList: [], //店铺列表

                /*分页接口参数*/
                start: 0,
                limit: 9,
                loading: false, //默认的隐藏加载提示
                listCompleted: false,  //数据列表加载完成
                showGoTop: false,//默认置顶按钮为隐藏
            }
        },
        created() {
            let self = this;
            self.sendHttp(res=>{
                self.shopList = res.data || [];
                self.pageLoaded = true;

                if(self.shopList.length < self.limit) self.listCompleted = true;
            });

            //同步购物车商品数量
            cartService.resumeCartGoods(self);
        },
        methods: {
            /**发送Http请求*/
            sendHttp(success, fail) {
                let self = this;
                api.sendHttp('getAllShopApi', {start: self.start, limit: self.limit}).then(
                    res =>{
                        if(typeof success === 'function') success(res);
                    }, res=>{
                        modal.toast(res.msg);
                        if(typeof fail === 'function') fail(res);
                });
            },

            /**无限滚动分页*/
            fetchMore() {
                this.$refs['list'].resetLoadmore(); //下一次滚动时强制触发loadmore

                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }
                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                api.sendHttp('getAllShopApi',{start: self.start, limit: self.limit}).then(
                    res => {
                        self.loading = false;
                        let _data = res.data || [];
                        if(_data.length < self.limit) self.listCompleted = true;
                        self.shopList = self.shopList.concat(_data);
                    }, res => {
                        self.loading = false;
                        modal.toast(res.msg);
                    }
                );
            },

            /**list滚动监听事件，动态的控制置顶按钮显示与隐藏*/
            onScroll(e) {
                let contentOffset = e.contentOffset;
                let offsetY = contentOffset.y;
                if(offsetY <= -4000){
                    if(!this.showGoTop) this.showGoTop = true;
                } else if(offsetY > -4000) {
                    if(this.showGoTop) this.showGoTop = false;
                }
            },
            /**跳转店铺*/
            goToShop(shop){
                let options = {
                    url: route.shop_index,
                    title: shop.s_name || '商家店铺',
                    params: {
                        shopType: shop.shoptype
                    }
                };
                navigator.push(options);
            }
        },
        destroyed(){
            cartService.removeResumeCartGoodsListener();
        }
    }
</script>
